package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.CoverFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.CoverFaultHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/8/11.
 */
@Service
public class CoverFaultHandleServiceImpl implements CoverFaultHandleService {
    @Resource
    private CoverFaultRepository coverFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getCoverFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer machineMaintenanceStatus) throws Exception {
        List<DTCoverFaultDto> dto4DTs = coverFaultRepository.getCoverFaultHandle4DT(dtRequestDto,actorIds, orgId,machineMaintenanceStatus);
        Integer count = coverFaultRepository.getCoverFaultHandleCount4DT(dtRequestDto,actorIds, orgId,machineMaintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTCoverFaultDto dtMachineFaultDto:dto4DTs){
            if(dtMachineFaultDto.getCoverMaintainerId()!=null){
                String maintainerName =userRepository.getUserNameByUserId(dtMachineFaultDto.getCoverMaintainerId());
                dtMachineFaultDto.setCoverMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addCoverMaintenanceContent(Integer machineFaultId, String machineMaintenanceContent) throws Exception {
        return coverFaultRepository.addCoverMaintenanceContent(machineFaultId,machineMaintenanceContent);
    }

    @Transactional
    public Integer finishCoverMaintenance(String machineMaintenancePhoto, Integer machineMaintainerId, String machineMaintenanceTime, String machineMaintenanceExplain, Integer machineFaultId) throws Exception {
        return coverFaultRepository.finishCoverMaintenance(machineMaintenancePhoto,machineMaintainerId,machineMaintenanceTime,machineMaintenanceExplain,machineFaultId);
    }


    public List<CoverFaultDto> getCoverFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return coverFaultRepository.getCoverFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public CoverFaultDto getCoverReceiptInfo4App(Integer machineFaultId) throws Exception{
        return coverFaultRepository.getMachineReceiptInfo4App(machineFaultId);
    }
}
